package com.erp.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.erp.shop.domain.bo.ErpSellerStorePriceTemplateBo;
import com.erp.shop.domain.vo.ErpSellerStorePriceTemplateVo;
import com.erp.shop.service.IErpSellerStorePriceTemplateService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.shop.api.domain.ErpSellerStorePriceTemplate;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 智能定价模板
 *
 * @author erp
 * @date 2023-05-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStorePriceTemplate")
public class ErpSellerStorePriceTemplateController extends BaseController {

	private final IErpSellerStorePriceTemplateService iErpSellerStorePriceTemplateService;

	@GetMapping("/sellerStorePriceTemplateById/{id}")
	public R<Object> sellerStorePriceTemplateById(@PathVariable("id") Long id) {
		ErpSellerStorePriceTemplate erpSellerStorePriceTemplate = iErpSellerStorePriceTemplateService.selectById(id);
		if (Objects.isNull(erpSellerStorePriceTemplate)){
			return R.fail(null);
		}else {
			return R.ok(erpSellerStorePriceTemplate);
		}
	}

	@GetMapping("/sellerStorePriceTemplateBySellerId/{sellerId}")
	public R<Object> sellerStorePriceTemplateBySellerId(@PathVariable("sellerId") Long sellerId) {
		ErpSellerStorePriceTemplate erpSellerStorePriceTemplate = iErpSellerStorePriceTemplateService.sellerStorePriceTemplateBySellerId(sellerId);
		if (Objects.isNull(erpSellerStorePriceTemplate)){
			return R.fail(null);
		}else {
			return R.ok(erpSellerStorePriceTemplate);
		}
	}



	@GetMapping("/selectList")
	public R<Object> selectList(ErpSellerStorePriceTemplateBo bo) {
		List<ErpSellerStorePriceTemplateVo> list = iErpSellerStorePriceTemplateService.queryList(bo);
		List<Map<String, Object>> resList = new ArrayList<>();
		list.forEach(vo -> {
			Map<String, Object> map = new HashMap<>();
			map.put("lable", vo.getTemplateName());
			map.put("value", vo.getId());
			resList.add(map);
		});

		return R.ok(resList);
	}

	@GetMapping("/selectListBySellerId")
	public R<Object> selectListBySellerId(String sellerId) {
		List<ErpSellerStorePriceTemplateVo> list = iErpSellerStorePriceTemplateService.selectListBySellerId(sellerId);
		List<Map<String, Object>> resList = new ArrayList<>();
		list.forEach(vo -> {
			Map<String, Object> map = new HashMap<>();
			map.put("lable", vo.getTemplateName());
			map.put("value", vo.getId());
			resList.add(map);
		});

		return R.ok(resList);
	}

	@GetMapping("/selectInfoById")
	public R<Object> selectInfoById(Long id) {
		return iErpSellerStorePriceTemplateService.selectInfoById(id);
	}

	/**
	 * 查询智能定价模板列表
	 */
	@SaCheckPermission("seller:sellerStorePriceTemplate:list")
	@GetMapping("/list")
	public TableDataInfo<ErpSellerStorePriceTemplateVo> list(ErpSellerStorePriceTemplateBo bo, PageQuery pageQuery) {
		return iErpSellerStorePriceTemplateService.queryPageList(bo, pageQuery);
	}

	/**
	 * 导出智能定价模板列表
	 */
	@SaCheckPermission("seller:sellerStorePriceTemplate:export")
	@Log(title = "智能定价模板", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(ErpSellerStorePriceTemplateBo bo, HttpServletResponse response) {
		List<ErpSellerStorePriceTemplateVo> list = iErpSellerStorePriceTemplateService.queryList(bo);
//		ExcelUtil.exportExcel(list, "智能定价模板", ErpSellerStorePriceTemplateVo.class, response);
	}

	/**
	 * 获取智能定价模板详细信息
	 *
	 * @param id 主键
	 */
	@SaCheckPermission("seller:sellerStorePriceTemplate:query")
	@GetMapping("/{id}")
	public R<ErpSellerStorePriceTemplateVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
		return R.ok(iErpSellerStorePriceTemplateService.queryById(id));
	}

	/**
	 * 新增智能定价模板
	 */
	@SaCheckPermission("seller:sellerStorePriceTemplate:add")
	@Log(title = "智能定价模板", businessType = BusinessType.INSERT)
	@RepeatSubmit()
	@PostMapping()
	public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStorePriceTemplateBo bo) {
		return toAjax(iErpSellerStorePriceTemplateService.insertByBo(bo));
	}

	/**
	 * 修改智能定价模板
	 */
	@SaCheckPermission("seller:sellerStorePriceTemplate:edit")
	@Log(title = "智能定价模板", businessType = BusinessType.UPDATE)
	@RepeatSubmit()
	@PutMapping()
	public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStorePriceTemplateBo bo) {
		return toAjax(iErpSellerStorePriceTemplateService.updateByBo(bo));
	}

	/**
	 * 删除智能定价模板
	 *
	 * @param ids 主键串
	 */
	@SaCheckPermission("seller:sellerStorePriceTemplate:remove")
	@Log(title = "智能定价模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
		return toAjax(iErpSellerStorePriceTemplateService.deleteWithValidByIds(Arrays.asList(ids), true));
	}
}
